import ilog.concert.IloException;
import ilog.concert.IloLinearNumExpr;
import ilog.concert.IloNumVar;
import ilog.concert.IloRange;
import ilog.cplex.IloCplex;

import java.util.*;

import static ilog.cplex.IloCplex.Param.Preprocessing.Symmetry;

public class Main {
    public static void main(String[] args) {
        try {
            IloCplex cplex = new IloCplex();

            // 设置参数以寻找多组解
//            cplex.setParam(IloCplex.IntParam.MIPSearch, IloCplex.MIPSearchType.Plugh);
//            cplex.setParam(IloCplex.IntParam.MIPStrongBranching, 0);

            // 定义变量、约束和目标函数（与你的具体问题相关）
            IloNumVar x = cplex.numVar(0, 10, "x");
            IloNumVar y = cplex.numVar(0, 10, "y");
            IloLinearNumExpr expr = cplex.linearNumExpr();
            expr.addTerm(2, x);
            expr.addTerm(1, y);
            cplex.addEq(expr, 6);

            // 这里我们可以设置一个简单的目标函数，例如最小化 x + y
            IloLinearNumExpr objectiveExpr = cplex.linearNumExpr();
            objectiveExpr.addTerm(1, x);
            objectiveExpr.addTerm(1, y);
            cplex.addMinimize(objectiveExpr);


            int solutionCount = 0;
            List<double[]> solutions = new ArrayList<>();
            while (true) {
                double[] solution = new double[2];
                if (cplex.solve()) {
                    solutionCount++;
                    solution[0] = cplex.getValue(x);
                    solution[1] = cplex.getValue(y);
                    solutions.add(solution);
                } else {
                   break;
                }
                cplex.addGe(x,  solution[0]+0.00001);
                cplex.addLe(x,  solution[0]-0.00001);
                cplex.addGe(y,  solution[1]+0.00001);
                cplex.addLe(y,  solution[1]-0.00001);

                if (solutions.size()== 2){
                    break;
                }
            }

            // 查看保存的解
            System.out.println("Found " + solutionCount + " solutions.");
            for (int i = 0; i < solutions.size(); i++) {
                System.out.println("Solution " + (i + 1) + ":");
                System.out.println("x = " + solutions.get(i)[0]);
                System.out.println("y = " + solutions.get(i)[1]);
            }

            cplex.end();
        } catch (IloException e) {
            e.printStackTrace();
        }
    }

}
